package com.leps.nq.bu.controller;

import cn.hutool.core.util.StrUtil;
import com.leps.nq.bu.entity.BuStation;
import com.leps.nq.bu.service.IBuStationService;
import com.leps.nq.bu.vo.StationQueryModel;
import com.leps.nq.bu.vo.StationVo;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 区块表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/bu/bu-station")
@Api(tags = "平台管理")
public class BuStationController {
    @Autowired
    private IBuStationService stationService;

    @GetMapping("/forSelect")
    @ApiOperation("平台下拉列表")
    public R<List<BuStation>> listForSelect(Integer areaId) {
        List<BuStation> list = this.stationService.listForSelect(areaId);
        return RBuilder.ok(list);
    }

    @GetMapping
    @ApiOperation("平台列表")
    public R<PageVo<StationVo>> list(StationQueryModel queryModel) {
        PageVo<StationVo> pageVo = this.stationService.pageByQueryModel(queryModel);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询平台信息")
    public R<BuStation> getById(@PathVariable Integer id) {
        return RBuilder.ok(this.stationService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加平台")
    @Log(title = "平台", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody BuStation station) {
        String stationName = station.getStationName();
        Long count = this.stationService.lambdaQuery().eq(BuStation::getStationName, stationName).count();
        if (count > 0) {
            String msg = StrUtil.format("系统中已存在 [ {} ] 这个平台，请不要重复添加。", stationName);
            throw new RuntimeException(msg);
        }
        boolean b = this.stationService.save(station);
        return RBuilder.ofBoolean(b);
    }

    @PutMapping
    @ApiOperation("更新平台")
    @Log(title = "平台", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody BuStation station) {
        boolean b = this.stationService.updateById(station);
        return RBuilder.ofBoolean(b);
    }

    @DeleteMapping
    @ApiOperation("删除平台")
    @Log(title = "平台", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Integer> stationIds) {
        boolean b = this.stationService.removeBatchByIds(stationIds);
        return RBuilder.ofBoolean(b);
    }
}
